package com.cn.lyl.service.impl;

import com.cn.lyl.dao.ServiceDao;
import com.cn.lyl.dao.ServiceTypeDao;
import com.cn.lyl.dao.ServiceTypeDependenceDao;
import com.cn.lyl.dao.impl.ServiceDaoImpl;
import com.cn.lyl.dao.impl.ServiceTypeDaoImpl;
import com.cn.lyl.dao.impl.ServiceTypeDependenceDaoImpl;
import com.cn.lyl.model.ServiceModel;
import com.cn.lyl.model.ServiceTypeDependenceModel;
import com.cn.lyl.model.ServiceTypeModel;
import com.cn.lyl.service.ServiceService;

import java.util.List;

public class ServiceServiceImpl implements ServiceService {
    @Override
    public List<ServiceModel> findAllService(ServiceModel serviceModel) {
        ServiceDao dao = new ServiceDaoImpl();
        return dao.findAll(serviceModel);
    }

    @Override
    public int getCount(ServiceModel serviceModel) {
        ServiceDao dao = new ServiceDaoImpl();
        return dao.countFindAll(serviceModel);
    }

    @Override
    public String addService(ServiceModel serviceModel) {
        ServiceDao serviceDao =new ServiceDaoImpl();
        ServiceTypeDao serviceTypeDao = new ServiceTypeDaoImpl();
        ServiceTypeDependenceDao serviceTypeDependenceDao = new ServiceTypeDependenceDaoImpl();
        ServiceTypeModel type = new ServiceTypeModel();
        if(serviceModel.getDependenceObj()==null){
            return serviceDao.add(serviceModel)>0?"success":"error";
        }
        int insert = serviceDao.add(serviceModel);
        type.setType((String) serviceModel.getDependenceObj());
        ServiceTypeModel byName = serviceTypeDao.findByName(type);
        ServiceModel newModel = serviceDao.findNewest();
        ServiceTypeDependenceModel model = new ServiceTypeDependenceModel();
        model.setServiceId(newModel.getId());
        model.setTypeId(byName.getId());
        return (serviceTypeDependenceDao.add(model)>0&&insert>0)?"success":"error";
    }

    @Override
    public String deleteService(ServiceModel serviceModel) {
        ServiceDao serviceDao =new ServiceDaoImpl();
        ServiceTypeDependenceDao serviceTypeDependenceDao = new ServiceTypeDependenceDaoImpl();
        ServiceTypeDependenceModel model = new ServiceTypeDependenceModel();
        model.setServiceId(serviceModel.getId());
        serviceTypeDependenceDao.delete(model);
        return serviceDao.delete(serviceModel)>0?"success":"error";
    }

    @Override
    public ServiceModel findInfo(int id) {
        ServiceDao serviceDao =new ServiceDaoImpl();
        return serviceDao.findById(id);
    }

    @Override
    public String changeInfo(ServiceModel serviceModel) {
        ServiceDao serviceDao =new ServiceDaoImpl();
        return serviceDao.update(serviceModel)>0?"success":"error";
    }
}
